package edu.uoc.tdp.pac4.client.Academic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Registration;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.AcademicManagementInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

/**
 * JPanel to show the options relative to registration management
 * @author Laia
 *
 */
/**
 * @author Laia
 *
 */
public class Registration_ViewRegistration extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	AcademicManagementInterface academicManagement;
	ClientConnectionHelper connectionHelper_ = null;
	private JTextField textField_2;
	private JTable table;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox<String> comboBox;
	private JPanel panel_1 = new JPanel();
	private JRadioButton rdtAcceptat ;
	private JRadioButton rbtAll;
	private JRadioButton rbtPendent;
	private JRadioButton rbtBaixa;

	
	/**
	 * Builds the JPanel
	 */

	public Registration_ViewRegistration(ClientConnectionHelper connectionHelper) {
		connectionHelper_ = connectionHelper;
		
		academicManagement = connectionHelper_.getAcademicManager();
	
		setBorder(new TitledBorder(null,
				LanguageUtils
						.getMessage("client.management.button.titleViewRegistration"),
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(6, 22, 619, 164);
		add(panel);
		panel.setLayout(null);

		JLabel lblNomDelCurs = new JLabel(
				LanguageUtils.getMessage("client.management.button.nameCourse"));
		lblNomDelCurs.setBounds(10, 17, 134, 14);
		panel.add(lblNomDelCurs);

		JLabel lblNIF = new JLabel(
				LanguageUtils
						.getMessage("client.management.button.nif"));
		lblNIF.setBounds(376, 17, 113, 14);
		panel.add(lblNIF);

		textField_2 = new JTextField();
		textField_2.setBounds(507, 14, 86, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);

		comboBox = new JComboBox<String>();
		comboBox.setBounds(133, 14, 120, 20);
		try {
			comboBox.setModel(posarModel());
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		panel.add(comboBox);

		 rbtAll = new JRadioButton(
					LanguageUtils.getMessage("client.management.button.all"));
		rbtAll.setBounds(133, 77, 81, 23);
		panel.add(rbtAll);
		buttonGroup.add(rbtAll);
		rbtAll.setSelected(true);
		
		 rdtAcceptat = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.accepted"));
		rdtAcceptat.setSelected(true);
		buttonGroup.add(rdtAcceptat);
		rdtAcceptat.setBounds(230, 77, 118, 23);
		panel.add(rdtAcceptat);

		 rbtBaixa = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.notActive"));
		buttonGroup.add(rbtBaixa);
		rbtBaixa.setBounds(500, 77, 81, 23);
		panel.add(rbtBaixa);

		 rbtPendent = new JRadioButton(
				LanguageUtils.getMessage("client.management.button.pending"));
		buttonGroup.add(rbtPendent);
		rbtPendent.setBounds(350, 77, 101, 23);
		panel.add(rbtPendent);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 197, 619, 210);
		add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
				LanguageUtils.getMessage("client.management.button.id"),
				LanguageUtils
				.getMessage("client.management.button.nif"),
				LanguageUtils
				.getMessage("client.management.button.name"),
				LanguageUtils
				.getMessage("client.management.button.lastname1"),
				LanguageUtils
				.getMessage("client.management.button.course"),
				LanguageUtils
						.getMessage("client.management.button.shift"),
						LanguageUtils
						.getMessage("client.management.button.status")
				
		}));
		scrollPane.add(table);
		scrollPane.setViewportView(table);

		JButton btnBuscar = new JButton(
				LanguageUtils.getMessage("client.management.button.search"));
		btnBuscar.setBounds(10, 130, 101, 23);
		panel.add(btnBuscar);
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					cercar();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		JButton btnDesar = new JButton(
				LanguageUtils.getMessage("client.management.button.acceptReg"));
		btnDesar.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				try {
					update();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		panel_1.setLayout(null);
		btnDesar.setBounds(10, 5, 246, 23);
		panel_1.add(btnDesar);

		JButton button = new JButton(
				LanguageUtils.getMessage("client.management.button.clean"));
		panel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField_2.setText("");
				rbtAll.setSelected(true);
				comboBox.setSelectedIndex(0);
				
			}
		});
		button.setBounds(121, 130, 132, 23);
		
		JLabel label = new JLabel((String) null);
		label.setBounds(293, 15, 121, 14);
		panel.add(label);
		
		JLabel lblEstatusMatrcula = new JLabel(
				LanguageUtils.getMessage("client.management.button.regStatus"));
		lblEstatusMatrcula.setBounds(10, 81, 121, 14);
		panel.add(lblEstatusMatrcula);
		


		panel_1.setBounds(6, 424, 619, 37);
		add(panel_1);

		

	}


	/**
	 * Searches and prints registrations
	 * @throws Exception 
	 * 
	 */
	public void cercar() throws Exception {
		
		String curs = "";
		curs = (String) comboBox.getSelectedItem();
		int cursId = 0;
		
		if (curs != null) {
			cursId = academicManagement.findCourseByName(curs).getCourseCode();
		}
				
		String nif = null;
		nif = textField_2.getText();
		
		
		int statusInt = -1;

		if (rdtAcceptat.isSelected()) {
			statusInt = 1;
		}
		if (rbtPendent.isSelected()) {
			statusInt = 0;
		}
		if (rbtBaixa.isSelected()) {
			statusInt = 2;
		}


		
		List<Registration> registrations;
		registrations = academicManagement.findRegistrations(nif, cursId, statusInt);
	
		Object[] columnNames = new String[] {
				LanguageUtils.getMessage("client.management.button.id"),
				LanguageUtils
				.getMessage("client.management.button.nif"),
				LanguageUtils
				.getMessage("client.management.button.name"),
				LanguageUtils
				.getMessage("client.management.button.lastname1"),
				LanguageUtils
				.getMessage("client.management.button.course"),
				LanguageUtils
						.getMessage("client.management.button.shift"),
						LanguageUtils
						.getMessage("client.management.button.status")
	
				
						};
		Object[][] data = new Object[registrations.size()][columnNames.length];

		//for each registration, gets the info about the group and the student.
		for (int i = 0; i < data.length; i++) {
			Registration registration = registrations.get(i);
			Group group = academicManagement.getGroup(registration.getGroupId());
			
			Usuari student = academicManagement.findStudent(registration.getUserId());
			
			String status = "";
	
			switch (registration.getStatus()) {
			case 0:
				status = LanguageUtils
						.getMessage("client.regandlog.rbtnlabel.pending");
				break;
			case 1:
				status =  LanguageUtils
						.getMessage("client.regandlog.rbtnlabel.accepted");
				break;
			case 2:
				status = LanguageUtils
						.getMessage("client.regandlog.rbtnlabel.declined");
				break;
				
			}
			
			data[i] = new Object[] { registration.getRegistrationId(), registration.getUserId(), student.getNombre(), student.getApellidos(),
				group.getName(), group.getShift().toString(), status};
				
		}

		TableModel model = new DefaultTableModel(data, columnNames);
		table.setModel(model);
	}
	
	
	/**
	 * Makes sure that a registration is selected and opens a Registration_AcceptRegistration window
	 * 
	 * @throws RemoteException
	 */
	public void update() throws Exception {
		
		

		int i = table.getSelectedRow();
		TableModel model2 = table.getModel();
		try {
			Integer identificador = (Integer) model2.getValueAt(i, 0);
			
			Registration matricula =  null;
			matricula = academicManagement.getRegistration(identificador);
			Group group = academicManagement.getGroup(matricula.getGroupId());	
			Registration_AcceptRegistration registration_acceptRegistration = new Registration_AcceptRegistration(matricula, group, connectionHelper_);
			registration_acceptRegistration.setVisible(true);
			
		} catch (ArrayIndexOutOfBoundsException e) {
			JOptionPane.showMessageDialog(null, LanguageUtils
					.getMessage("client.management.error.selecreg"),
					LanguageUtils.getMessage("client.management.error"),
					JOptionPane.ERROR_MESSAGE);
			return;
		}

	}
	
	/**
	 * Populates the comboBoxModel with names of courses.
	 * 
	 * @return
	 * @throws RemoteException 
	 */
	public DefaultComboBoxModel<String> posarModel() throws RemoteException {
		List<Course> courses;
		courses = academicManagement.listCourses();
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
		model.addElement(null);
		for (int i = 0; i < courses.size(); i++) {
			model.addElement(courses.get(i).getCourseName());
		}
		return model;

	}
}
